import GParam from "../../../Core/GEvent/GParam";
import GViewBase from "../../../Core/GView/GViewBase";
import { BLESSEND_INFO, ITEM_DETAIL_FLAG, ITEM_TYPE } from "../../Common/Define";
import { JXColor } from "../../Common/JXColor";
import { L } from "../../Common/Language";
import UICreate from "../../Common/UICreate";
import { Res } from "../../Common/UIResources";
import { JXLocales } from "../../Common/Zh";
import { REquireItem } from "../../Data/RunTime/RunRaw/RItemAttach";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";
import JXItem from "../Common/JXItem";
import PropertyItem from "./PropertyItem";

const { ccclass, menu, property } = cc._decorator;

@ccclass @menu('View/Home/StrengthenView')
export default class StrengthenView extends GViewBase {
    @property({ type: cc.Sprite, tooltip: "强化文案" }) strLab: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "强化后背景" }) resBg: cc.Sprite = null;
    @property({ type: cc.Node, tooltip: "显示属性节点" }) propertyParent: cc.Node = null;
    @property({ type: cc.Node, tooltip: "图标" }) jxPos: cc.Node = null;
    @property({ type: cc.Label, tooltip: "强化信息" }) info: cc.Label = null;
    private _data: REquireItem = null;
    _qhItem: JXItem = null;

    onGStart(data: REquireItem, num: number) {
        // console.log('强化成功', data);
        this._data = data;
        let EquipAddInfo: EquipAddInfo = {
            equipId: this._data.insId,
            exp: this._data.sLv - 1
        }

        if (this._data.sLv > 0) {
            this.info.string = '+' + this._data.sLv + '  ' + data.itemData.raw.name;
        } else {
            this.info.string = '' + data.itemData.raw.name;
        }

        this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab: cc.Prefab) => {
            if (!this._qhItem) {
                let comp = this.assetImpl.addGChild<JXItem>(prefab);
                comp.node.parent = this.jxPos;
                comp.node.setPosition(0, 0);
                this._qhItem = comp;
            }
            let btn = new RJXItem([data.equipId, ITEM_TYPE.EQUIPDATA, 1]);
            this._qhItem.setView(btn, ITEM_DETAIL_FLAG.TPL | ITEM_DETAIL_FLAG.NO_NUM);
        })

        if (num == BLESSEND_INFO.strSus) {
            this.strSus(EquipAddInfo);
            this.assetImpl.spriteFrame(this.strLab, Res.bigpic.main + "sus");
            this.assetImpl.spriteFrame(this.resBg, Res.bigpic.main + "susBg");

        } else if (num == BLESSEND_INFO.strFail) {
            this.assetImpl.spriteFrame(this.strLab, Res.bigpic.main + "failLab");
            this.assetImpl.spriteFrame(this.resBg, Res.bigpic.main + "failBg");
            this.jxPos.setPosition(-135, 45);
            this.info.node.setPosition(49, 48);
        } else if (num == BLESSEND_INFO.synthesis) {
            this.assetImpl.spriteFrame(this.strLab, Res.bigpic.main + "synthesisTitle");
            this.assetImpl.spriteFrame(this.resBg, Res.bigpic.main + "susBg");
            if (this._data.sLv > 0) {
                this.strSus(EquipAddInfo);
            }
        }
    }

    protected strSus(EquipAddInfo: EquipAddInfo) {
        //上一级的强化属性
        let proptsEquip = GameMgr.RBagData.ontopEquip(null, new GParam(EquipAddInfo));
        //初始属性
        var propts = GameMgr.equipData.getRaw<SEquipDataRaw>(this._data.equipId).property;
        //强化成功后的属性
        let proptscurEquip = this._data.propts;
        /**索引值 */
        let arr1 = [];
        /**节点 */
        let arr2 = [];
        proptsEquip.forEach((value, index) => {
            value -= propts[index];
            if (value == 0) {
                return;
            }
            this.assetImpl.font(Res.font.mnpw, (font) => {
                let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                let str = L(JXLocales.propts["info" + index], '+' + numStr);
                let lab = UICreate.label(str);
                lab.fontSize = 21;
                lab.lineHeight = 21;
                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                lab.node.anchorX = 0;
                lab.node.color = JXColor.Cffffff;
                lab.node.addComponent(cc.LabelOutline);
                let line = lab.node.getComponent(cc.LabelOutline);
                line.color = cc.color(8, 89, 152, 255);
                line.width = 2;
                lab.font = font;
                arr1.push(index);
                arr2.push(lab.node);
            })

        })

        /**索引值 */
        let arr3 = [];
        /**节点 */
        let arr4 = [];
        proptscurEquip.forEach((value, index) => {
            value -= propts[index];
            if (value == 0) {
                return;
            }
            this.assetImpl.font(Res.font.mnpw, (font) => {
                let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                let str = L(JXLocales.propts["info" + index], '+' + numStr);
                let lab = UICreate.label(str);
                lab.fontSize = 21;
                lab.lineHeight = 21;
                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                lab.node.anchorX = 0;
                lab.node.color = JXColor.Cffffff;
                arr3.push(index);
                arr4.push(lab.node);
                lab.node.addComponent(cc.LabelOutline);
                let line = lab.node.getComponent(cc.LabelOutline);
                line.color = cc.color(8, 89, 152, 255);
                line.width = 2;
                lab.font = font;
            })

        })

        this.assetImpl.prefab(Res.prefab.vw.home.propertyItem, (prefab) => {
            for (let i = 0; i < arr3.length; i++) {
                let comp = this.assetImpl.addGChild<PropertyItem>(prefab);
                if (this._data.sLv - 1 <= 0) {
                    this.assetImpl.font(Res.font.mnpw, (font) => {
                        let str = L(JXLocales.propts["info" + i], '+' + 0);
                        let lab = UICreate.label(str);
                        lab.node.addComponent(cc.LabelOutline);
                        let line = lab.node.getComponent(cc.LabelOutline);
                        lab.node.anchorX = 0;
                        line.color = cc.color(8, 89, 152, 255);
                        line.width = 2;
                        lab.font = font;
                        // comp.node.anchorX = 0;
                        comp.node.getChildByName('topProperty').addChild(lab.node);
                        comp.node.getChildByName('nextProperty').addChild(arr4[i]);
                    })

                } else {
                    if (!arr2[i]) {
                        this.assetImpl.font(Res.font.mnpw, (font) => {
                            let str = L(JXLocales.propts["info" + i], '+' + 0);
                            let lab = UICreate.label(str);
                            lab.node.addComponent(cc.LabelOutline);
                            lab.node.anchorX = 0;
                            let line = lab.node.getComponent(cc.LabelOutline);
                            line.color = cc.color(8, 89, 152, 255);
                            line.width = 2;
                            lab.font = font;
                            // comp.node.anchorX = 0;
                            comp.node.getChildByName('topProperty').addChild(lab.node);
                        })

                    } else {
                        // comp.node.anchorX = 0;
                        comp.node.getChildByName('topProperty').addChild(arr2[i]);
                    }
                    // comp.node.anchorX = 0;
                    comp.node.getChildByName('nextProperty').addChild(arr4[i]);
                }
                this.propertyParent.addChild(comp.node);
            }
        })

        // console.log('具体属性', arr2, arr4);
    }

    close() {
        this.onClose();
    }
}
